<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
    width="600px"
  >
    <el-form :model="form" size="mini" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="属性名" prop="name">
        <el-input v-model="form.name" placeholder="属性名"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="属性类型" prop="attrType">
            <el-select disabled v-model="form.attrType" placeholder="请选择">
              <el-option label="规格参数" :value="1"></el-option>
              <el-option label="销售属性" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="值类型" prop="valueType">
            <el-select v-model="form.valueType" placeholder="请选择">
              <el-option label="允许多个值" :value="0"></el-option>
              <el-option label="只能单个值" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="可选值" prop="optionalValue">
        <el-select
          style="width: 100%"
          v-model="form.optionalValue"
          multiple
          filterable
          allow-create
          placeholder="请输入内容"
        ></el-select>
      </el-form-item>
      <el-form-item label="属性图标" prop="icon">
        <material-library :width="100" :height="100" :num="1" v-model="form.icon"></material-library>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属分类" prop="categoryId">
            <category-selector :categoryPath.sync="categoryPath"></category-selector>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属分组" prop="attrGroupId" v-if="type === 1">
            <el-select ref="groupSelect" v-model="form.attrGroupId" placeholder="请选择">
              <el-option
                v-for="item in attrGroups"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="可检索" prop="searchType" v-if="type === 1">
            <el-switch
              v-model="form.searchType"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="快速展示" prop="quickShow" v-if="type === 1">
            <el-switch
              v-model="form.quickShow"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="enable">
            <el-switch
              v-model="form.enable"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" icon="el-icon-circle-check" @click="submitForm()">确定</el-button>
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategorySelector from '../../common/categorySelector.vue'
import { listAllAttrGroup} from '@/api/product/attrGroup'
import { getById, saveAttr, updateAttr} from '@/api/product/attr'
import ImageLibrary from '@/components/ImageLibrary/index.vue'
import MaterialLibrary from "@/components/ImageLibrary/index.vue";

export default {
  data() {
    return {
      visible: false,
      form: {},
      categoryPath: [],
      attrGroups: [],
      dataRule: {
        name: [
          { required: true, message: '属性名不能为空', trigger: 'blur' }
        ],
        valueType: [
          {
            required: true, message: '值类型不能为空', trigger: 'blur'
          }
        ],
        attrType: [
          { required: true, message: '属性类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {MaterialLibrary, ImageLibrary, CategorySelector },
  props: {
    type: {
      type: Number,
      default: 1
    }
  },
  watch: {
    categoryPath(path) {
      this.attrGroups = []
      delete this.form.attrGroupId
      this.form.categoryId = path[path.length - 1]
      if (path && path.length === 3) {
        listAllAttrGroup({categoryId: path[path.length - 1]}).then(res => {
          if (res.success) {
            this.attrGroups = res.data
          }
        })
      } else if (path.length === 0) {
        this.form.categoryId = ''
      } else {
        this.$message.error('请选择正确的分类')
        this.form.categoryId = ''
      }
    }
  },
  methods: {
    edit(id) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        getById(id).then(res => {
          let groupId = res.data.attrGroupId
          if (res.success) {
            this.form = res.data
            this.categoryPath = res.data.categoryPath
            this.form.optionalValue = res.data.optionalValue
            this.$nextTick(() => {
              this.form.attrGroupId = groupId
            })
          }
        })
      })
    },
    add(categoryPath) {
      this.visible = true
      this.form = {}
      this.categoryPath = null
      if (categoryPath) {
        this.categoryPath = categoryPath
      }
      this.form.attrType = this.type
    },
    submitForm() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            saveAttr(this.form).then(res => {
              if (res.success) {
                this.visible = false
                this.$emit('ok')
                this.$message.success('操作成功')
              }
            })
          } else {
            updateAttr(this.form).then(res => {
              if (res.success) {
                this.visible = false
                this.$emit('ok')
                this.$message.success('操作成功')
              }
            })
          }
        }
      })
    },
    dialogClose() {
      this.catelogPath = []
    }
  }
}
</script>
